<?php

namespace Frame\LLOneBot;


class Message
{

    /**
     * @var string 存储原始消息数据的JSON字符串
     */
    private $retult;

    /**
     * @var array 解析后的消息数据数组
     */
    private $data;


    public function __construct()
    {
        // 示例数据，实际应用中应从外部获取
        if (isset($_GET['msg'])) {
            $this->retult = '{"self_id":991882419,"user_id":2772655946,"time":1725895986,"message_id":-2147465388,"real_id":-2147465388,"message_seq":-2147465388,"message_type":"group","sender":{"user_id":2772655946,"nickname":"蓝桉","card":"小杰","role":"owner"},"raw_message":"菜单","font":14,"sub_type":"normal","message":[{"data":{"text":"菜单"},"type":"text"}],"message_format":"array","post_type":"message","group_id":808540150}';//群聊
        } else {
            $this->retult = file_get_contents('php://input');

        }
        $this->data = json_decode($this->retult, true);
        file_put_contents(__DIR__ . '/Msg.log', $this->retult . "\n", FILE_APPEND);
        print_r($this->data);
    }


    /**
     * 获取消息数组
     * @return array
     */
    public function get()
    {
        return [
            'type' => $this->data['message_type'], // 消息类型
            'uin' => $this->data['user_id'], // 操作者QQ
            'bot' => $this->data['self_id'], // 机器人QQ
            'gin' => $this->data['group_id'], // 群号
            'msg' => isset(($_GET['msg'])) ? $_GET['msg'] : $this->data['raw_message'],
            'real_id' => $this->data['real_id'],
            //            'nick' => $this->data['sender']['nickname'], // 操作者昵称
        ];
    }
}
